package com.mgface.framework.springboot.web;

import java.time.LocalDateTime;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Controller
public class HelloController implements ExitCodeGenerator {
	private final static Logger log = LogManager.getLogger(HelloController.class);
	@Value("${system.name:mgface}") // 属性值来源于配置文件参数
	private String projectName;
	@Value("${random.int[10,100]}")
	private int randomValue;

	@RequestMapping(value = "/{date}", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE,MediaType.TEXT_PLAIN_VALUE }, method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseStatus(code = HttpStatus.OK)
	@ResponseBody
	public String home(HttpServletRequest request, @PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") Date date) {
		System.out.println("date:" + date);
		log.info("系统名称:" + projectName + ",随机数:" + randomValue);
		log.info("访问IP:" + request.getRemoteAddr());
		// return "hello:" + Instant.now().atZone(ZoneId.of("Asia/Shanghai"));
		return "hello:" + LocalDateTime.now();

	}

	@RequestMapping(value = "/404", method = { RequestMethod.GET, RequestMethod.POST })
	public String show() {
		return "404";
	}

	@Override
	public int getExitCode() {
		return 1000;
	}
}
